package com.example.administrator.hellocomp;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.time.Instant;


public class MusicService extends Service {
    MediaPlayer mPlayer;
    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate(){
        super.onCreate();
        if(mPlayer==null)
        {
            mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.first);
            mPlayer.setLooping(true);
        }
    }
//    @Override
//    public int onStartCommand(Intent instant,int flags,int startId)
//    {
//        mPlayer.start();
//        return super.onStartCommand(instant,flags,startId);
//    }
    @Override
    public void onStart(Intent intent,int startId)
    {
        super.onStart(intent,startId);
        if(intent!=null)
        {
            Bundle bundle = intent.getExtras();
            int op = bundle.getInt("msg");
            switch (op)
            {
                case 1:
                    if(!mPlayer.isPlaying()){
                        mPlayer.start();
                    }
                    break;
                case 2:
                    if(mPlayer.isPlaying())
                    {
                        mPlayer.pause();
                    }
                    break;
                case 3:
                    if(mPlayer!=null)
                    {
                        mPlayer.stop();
                        try{
                            mPlayer.prepare();

                        }catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        mPlayer.seekTo(0);
                        mPlayer.start();

                    }
                    break;
            }
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if(mPlayer!=null)
        {
            mPlayer.stop();
            mPlayer.release();
        }
    }


}
